/*
 * JenaOntologyManager.java
 *
 * Created on 21 de Novembro de 2007, 00:27
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */
package org.ontoscheduler.jenaontologymanager;

import com.hp.hpl.jena.ontology.Individual;
import com.hp.hpl.jena.ontology.OntModel;
import com.hp.hpl.jena.ontology.OntModelSpec;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStream;
import org.ontoscheduler.concepts.ActionContribution;
import org.ontoscheduler.concepts.ActionUniversalFO;
import org.ontoscheduler.concepts.ActionUniversalHO;
import org.ontoscheduler.concepts.Agent;
import org.ontoscheduler.concepts.AppointmentGoal;
import org.ontoscheduler.concepts.InternalCommitment;
import org.ontoscheduler.concepts.SocialCommitment;

/**
 *
 * @author joaoolavo
 */
public class JenaOntologyManager {

    private static String ONTOSCHED_DIR = "/home/joaoolavo/UFES/Projeto Final/repositorio/OntoScheduler/";
    private static String ONTOSCHED_FILE = "ufo-c_commitments.owl";
    public String ns = "http://www.ufo.org/ufo-c/commitments.owl#";
    private static OntModel m;
    public ActionContribution actionContribution;
    public ActionUniversalFO actionUniversalFO;
    public ActionUniversalHO actionUniversalHO;
    public Agent agent;
    public SocialCommitment socialCommitment;
    public InternalCommitment internalCommitment;
    public AppointmentGoal appointmentGoal;

    /**
     * Creates a new instance of JenaOntologyManager
     */
    public JenaOntologyManager() {
        readOntology();
        startConcepts();
//        testes();
    }

    public JenaOntologyManager(File ontFile) {
        readOntology(ontFile);
        startConcepts();
    }
    
    public void readOntology() {
        m = ModelFactory.createOntologyModel(OntModelSpec.OWL_DL_MEM);
        m.read("file:" + ONTOSCHED_DIR + ONTOSCHED_FILE);
    }
    
    public void readOntology(File ontFile) {
        m = ModelFactory.createOntologyModel(OntModelSpec.OWL_DL_MEM);
        m.read("file:" + ontFile);
    }

    public void saveOntology(File ontFile) {
//        File f = new File("/home/joaoolavo/test.owl");
        try{
            OutputStream fOutStr = new FileOutputStream(ontFile);
            m.write(fOutStr);
        }
        catch(FileNotFoundException fnf){
            System.err.println("Arquivo não encontrado.");
            System.exit(1);
        }
    }

    public void startConcepts() {
        agent = new Agent(m);
        actionContribution = new ActionContribution(m);
        actionUniversalFO = new ActionUniversalFO(m);
        actionUniversalHO = new ActionUniversalHO(m);
        internalCommitment = new InternalCommitment(m);
        socialCommitment = new SocialCommitment(m);
        appointmentGoal = new AppointmentGoal(m);
    }

    public void testes() {
        Individual x1 = agent.addIndividual("Paula Baião Machado de Vasconcelos bla bla bla bla asdasdf");
        Individual x2 = agent.addIndividual("João Olavo Baião de Vasconcelos");
        Individual x3 = agent.addIndividual("Ana Luiza Miranda Dantas");
        Individual x7 = actionUniversalHO.addIndividual("Tipo Festa Formatura");
        Individual x4 = actionUniversalHO.addIndividual("Tipo Festa de Aniversario");
        Individual x6 = actionUniversalFO.addIndividual("Festa de Aniversario");
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        JenaOntologyManager jena = new JenaOntologyManager();
    }
}

//        Iterator i = jena.agent.listIndividuals();
//        while(i.hasNext()){
//            Individual ind = (Individual) i.next();
//            System.out.println(ind.getURI());
//        }
//        Iterator i = main.tipoDeAgenda.listIndividuals();
//        while(i.hasNext()){
//            Individual x = (Individual) i.next();
//            System.out.println(main.tipoDeAgenda.getIndividualName(x) + ":");
//            Iterator y = main.tipoDeAgenda.listAggregates(x);
//            while(y.hasNext()){
//                RDFNode val = (RDFNode) y.next();
//                System.out.println(val.asNode().toString());
//            }
//        }
//    private String getDateTime() {
//        DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
//        Date date = new Date();
//        return dateFormat.format(date);
//    }
//        #############################################################################################
//        Listando os individuos de uma Classe
//        OntClass c = m.getOntClass(ns + "Compromisso");
//        for(Iterator i = c.listInstances(); i.hasNext();){
//            OntResource r = (OntResource) i.next();
//            System.out.print(r.getLocalName());
//        }

//        #############################################################################################
//        Listando todas as classes da ontologia
//        for(Iterator i = m.listClasses(); i.hasNext(); ){
//            OntClass c = (OntClass) i.next();
//            if(!c.isAnon())
//                System.out.println( c.getLocalName() + " " + c.getURI());
//        }
//        #############################################################################################
//        Duas maneiras de criar um individuo para uma classe. Via o modelo ou via a classe.
//        OntClass compr = m.getOntClass( ns + "Compromisso" );
//        Individual inst = compr.createIndividual(ns + "compr2");
//        Individual inst2 = m.createIndividual(ns + "compr3", compr);
//        #############################################################################################
//        Imprimindo todos as restricoes ONLY de uma classe
//        for (Iterator i = compr.listSuperClasses( true ); i.hasNext(); ) {
//          OntClass c = (OntClass) i.next();
//
//          if (c.isRestriction()) {
//            Restriction r = c.asRestriction();
//
//            if (r.isAllValuesFromRestriction()) {
//              AllValuesFromRestriction av = r.asAllValuesFromRestriction();
//              System.out.println( "AllValuesFrom class " +
//                                  av.getAllValuesFrom().getLocalName() +
//                                  " on property " + av.getOnProperty().getLocalName() );
//            }
//          }
//        }

